<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>云服务器搭建必备教程 | Jin Tian</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  
  
  
  
  <meta name="description" content="网站，APP上云必备套路，本文针对ubuntu以及使用nginx和django建站的伙伴们">
<meta property="og:type" content="article">
<meta property="og:title" content="云服务器搭建必备教程">
<meta property="og:url" content="http://yoursite.com/2017/02/03/云服务器搭建必备教程/index.html">
<meta property="og:site_name" content="Jin Tian">
<meta property="og:description" content="网站，APP上云必备套路，本文针对ubuntu以及使用nginx和django建站的伙伴们">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2017-03-21T09:39:09.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="云服务器搭建必备教程">
<meta name="twitter:description" content="网站，APP上云必备套路，本文针对ubuntu以及使用nginx和django建站的伙伴们">
  
    <link rel="alternate" href="/atom.xml" title="Jin Tian" type="application/atom+xml">
  

  

  <link rel="icon" href="/css/images/mylogo.jpg">
  <link rel="apple-touch-icon" href="/css/images/mylogo.jpg">
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link href="https://fonts.googleapis.com/css?family=Open+Sans|Montserrat:700" rel="stylesheet" type="text/css">
  <link href="https://fonts.googleapis.com/css?family=Roboto:400,300,300italic,400italic" rel="stylesheet" type="text/css">
  <link href="//cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">
  <style type="text/css">
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/9749f0/00000000000000000001008f/27/l?subset_id=2&fvd=n5) format("woff2");font-weight:500;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/90cf9f/000000000000000000010091/27/l?subset_id=2&fvd=n7) format("woff2");font-weight:500;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/8a5494/000000000000000000013365/27/l?subset_id=2&fvd=n4) format("woff2");font-weight:lighter;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/d337d8/000000000000000000010095/27/l?subset_id=2&fvd=i4) format("woff2");font-weight:400;font-style:italic;}</style>
  <link rel="stylesheet" href="/css/style.css">

  <script src="/js/jquery-3.1.1.min.js"></script>
  <script src="/js/bootstrap.js"></script>

  <!-- Bootstrap core CSS -->
  <link rel="stylesheet" href="/css/bootstrap.css" >

  
    <link rel="stylesheet" href="/css/dialog.css">
  

  

  
    <link rel="stylesheet" href="/css/header-post.css" >
  

  
  
  
    <link rel="stylesheet" href="/css/vdonate.css" >
  

</head>



  <body data-spy="scroll" data-target="#toc" data-offset="50">


  
  <div id="container">
    <div id="wrap">
      
        <header>

    <div id="allheader" class="navbar navbar-default navbar-static-top" role="navigation">
        <div class="navbar-inner">
          
          <div class="container"> 
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
              <span class="sr-only">Toggle navigation</span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
            </button>

            
              <a class="brand" style="
                 border-width: 0px;  margin-top: 0px;"  
                href="#" data-toggle="modal" data-target="#myModal" >
                  <img width="124px" height="124px" alt="Hike News" src="/css/images/mylogo.jpg">
              </a>
            
            
            <div class="navbar-collapse collapse">
              <ul class="hnav navbar-nav">
                
                  <li> <a class="main-nav-link" href="/">首页</a> </li>
                
                  <li> <a class="main-nav-link" href="/archives">归档</a> </li>
                
                  <li> <a class="main-nav-link" href="/categories">分类</a> </li>
                
                  <li> <a class="main-nav-link" href="/tags">标签</a> </li>
                
                  <li> <a class="main-nav-link" href="/about">关于</a> </li>
                
                  <li> <a class="main-nav-link" href="http://luoli-luoli.com/chat">chat</a> </li>
                
                  <li><div id="search-form-wrap">

    <form class="search-form">
        <input type="text" class="ins-search-input search-form-input" placeholder="" />
        <button type="submit" class="search-form-submit"></button>
    </form>
    <div class="ins-search">
    <div class="ins-search-mask"></div>
    <div class="ins-search-container">
        <div class="ins-input-wrapper">
            <input type="text" class="ins-search-input" placeholder="请输入关键词..." />
            <span class="ins-close ins-selectable"><i class="fa fa-times-circle"></i></span>
        </div>
        <div class="ins-section-wrapper">
            <div class="ins-section-container"></div>
        </div>
    </div>
</div>
<script>
(function (window) {
    var INSIGHT_CONFIG = {
        TRANSLATION: {
            POSTS: '文章',
            PAGES: '页面',
            CATEGORIES: '分类',
            TAGS: '标签',
            UNTITLED: '(无标题)',
        },
        ROOT_URL: '/',
        CONTENT_URL: '/content.json',
    };
    window.INSIGHT_CONFIG = INSIGHT_CONFIG;
})(window);
</script>
<script src="/js/insight.js"></script>

</div></li>
            </div>
          </div>
                
      </div>
    </div>

</header>



      
            
      <div id="content" class="outer">
        
          <section id="main" style="float:none;"><article id="post-云服务器搭建必备教程" style="width: 75%; float:left;" class="article article-type-post" itemscope itemprop="blogPost" >
  <div id="articleInner" class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="thumb" class="article-title" itemprop="name">
      云服务器搭建必备教程
    </h1>
  

      </header>
    
    <div class="article-meta">
      
	<a href="/2017/02/03/云服务器搭建必备教程/" class="article-date">
	  <time datetime="2017-02-03T02:15:38.000Z" itemprop="datePublished">2017-02-03</time>
	</a>

      
      
	<a class="article-views">
	<span id="busuanzi_container_page_pv">
		阅读量<span id="busuanzi_value_page_pv"></span>
	</span>
	</a>

    </div>
    <div class="article-entry" itemprop="articleBody">
      
        <p>网站，APP上云必备套路，本文针对ubuntu以及使用nginx和django建站的伙伴们<br><a id="more"></a></p>
<blockquote>
<p>本文由金天原创，欢迎转载，请保留这段版权信息，enjoy :)</p>
</blockquote>
<h3 id="上云自动化脚本"><a href="#上云自动化脚本" class="headerlink" title="上云自动化脚本"></a>上云自动化脚本</h3><p>这里说的自动化脚本指的是搭建nginx+django+gunicorn+supervisor+postgreslq的脚本，只需要执行：<br><figure class="highlight vim"><table><tr><td class="code"><pre><div class="line"><span class="built_in">mkdir</span> Deploy</div><div class="line"><span class="keyword">cd</span> Deploy</div><div class="line">wget http<span class="variable">s:</span>//github.<span class="keyword">com</span>/jinfagang/UbuntuScripts/raw/master/FreshUbuntuSetup/django_web_server_setup.<span class="keyword">sh</span> | <span class="keyword">sh</span></div></pre></td></tr></table></figure></p>
<p>执行之后会做以下事情：</p>
<ul>
<li>更换pip的源，会在当前home目录下新建<code>./pip/pip.cnf</code>文件，如果之后不行请把这个文件移到／root目录下</li>
<li>apt安装必要的包：postgresql，libpg-dev, nginx, supervisor等</li>
<li>安装python包：django, restframework, gunicorn, psycopg2, pillow</li>
<li>把gunicorn，nginx，supervisor的示例配置文件放到<code>~/SampleConfs</code>文件夹下，分别移到对应的目录下配置即可nginx放到<code>/etc/nginx/sites-enabled</code>, supervisor放到<code>/etc/supervisor/conf.d/</code>, gunicorn放到django工程根目录，按照需求修改即可</li>
</ul>
<h3 id="防火墙设置"><a href="#防火墙设置" class="headerlink" title="防火墙设置"></a>防火墙设置</h3><p>这是服务器和本地不同的地方，有时候你启动nginx就是无法访问可能的原因就是防火墙的问题，防火墙你不用管什么iptables，ubuntu下直接操作ufw即可：<br><figure class="highlight bash"><table><tr><td class="code"><pre><div class="line">sudo ufw allow 80</div><div class="line">sudo ufw <span class="built_in">enable</span></div><div class="line">sudo ufw status</div></pre></td></tr></table></figure></p>
<p>然后配置nginx，哦对了，还有一步，虽然服务器的防火墙配置了，但是你的云管理中心的安全组也要配置，不知道阿里云有没有，腾讯云是有安全组的，你要在安全组中增加开放80端口的规则，或者直接用默认的开放所有端口。</p>
<h3 id="nginx配置"><a href="#nginx配置" class="headerlink" title="nginx配置"></a>nginx配置</h3><p>最后nginx配置也很蛋疼啊，首先要确保80端口打开了，查看80端口是否被nginx监听：<br><figure class="highlight vim"><table><tr><td class="code"><pre><div class="line">netstat -ntpl</div><div class="line"><span class="keyword">ps</span> -A|<span class="keyword">grep</span> nginx</div></pre></td></tr></table></figure></p>
<p>这个你可以看到nginx是否开启，以及80端口是否被监听，一般如果你的nginx正确的监听了80端口，你应该可以用wget下载到本地的index网页：<br><figure class="highlight css"><table><tr><td class="code"><pre><div class="line"><span class="selector-tag">wget</span> 127<span class="selector-class">.0</span><span class="selector-class">.0</span><span class="selector-class">.1</span></div></pre></td></tr></table></figure></p>
<p>这时候会下载你的网页，但是如果wget外网ip没有反应，那么很大的问题就是你的云服务器的安全组没有打开。</p>
<h1 id="UPDATE-上云的一些错误集锦"><a href="#UPDATE-上云的一些错误集锦" class="headerlink" title="[UPDATE] 上云的一些错误集锦"></a>[UPDATE] 上云的一些错误集锦</h1><p>虽然上面基本上囊括了基本的上云思路，但是还是有些细节问题需要注意。下面一一记录。</p>
<ul>
<li><p>postgresql忘记了密码<br>这个很蛋疼，二话不说，直接删除现有的user，重新新建并重制密码：</p>
<figure class="highlight ebnf"><table><tr><td class="code"><pre><div class="line"><span class="attribute">dropuser anyuser</span></div><div class="line">createuser -P newuser</div></pre></td></tr></table></figure>
</li>
<li><p>腾讯云上独有的编码问题<br>当你发现新建数据库的时候又是C的编码会很操蛋，那么新建表的时候强行设置编码:</p>
<figure class="highlight mathematica"><table><tr><td class="code"><pre><div class="line">createdb -l en_US.UTF<span class="number">-8</span> -<span class="keyword">E</span> UTF8 -<span class="keyword">O</span> root newdatabase</div></pre></td></tr></table></figure>
</li>
<li><p>依旧是编码问题<br>如果你ostgreslq移植无法变成utf-8，那么后面会很蛋疼，这样你修改系统的locale设置，一般来说是这样的：</p>
<figure class="highlight autohotkey"><table><tr><td class="code"><pre><div class="line">```</div><div class="line">然后写在postgresql，再重新安装：</div></pre></td></tr></table></figure>
</li>
</ul>
<p>sudo apt-get -y remove –purge postgresql-9.5<br>sudo apt install postgresql<br><figure class="highlight"><table><tr><td class="code"><pre><div class="line">最后用postgres账户新建数据库，把所有者设为你的用户名：</div></pre></td></tr></table></figure></p>
<p>sudo -u postgres createdb -O root oumenglite<br>```<br>这样基本上解决了数据库问题</p>

      
    </div>
    <footer class="article-footer">
      
        <div id="donation_div"></div>

<script src="/js/vdonate.js"></script>
<script>
var a = new Donate({
  title: '骚年，加个好友打赏一下啊，现在连泡面都吃不起了啊', // 可选参数，打赏标题
  btnText: '打赏支持', // 可选参数，打赏按钮文字
  el: document.getElementById('donation_div'),
  wechatImage: 'https://i.loli.net/2017/09/27/59cb048ba6838.jpeg',
  alipayImage: 'https://i.loli.net/2017/09/27/59cb049cd0951.jpeg'
});
</script>
      
      
        
	<div id="comment">
		<!-- 来必力City版安装代码 -->
		<div id="lv-container" data-id="city" data-uid="MTAyMC8zMDA5MC82NjQ1">
		<script type="text/javascript">
		   (function(d, s) {
		       var j, e = d.getElementsByTagName(s)[0];

		       if (typeof LivereTower === 'function') { return; }

		       j = d.createElement(s);
		       j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
		       j.async = true;

		       e.parentNode.insertBefore(j, e);
		   })(document, 'script');
		</script>
		<noscript>为正常使用来必力评论功能请激活JavaScript</noscript>
		</div>
		<!-- City版安装代码已完成 -->
	</div>



      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2017/02/20/三行代码Tensorflow入门/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">上一篇</strong>
      <div class="article-nav-title">
        
          三行代码Tensorflow入门
        
      </div>
    </a>
  
  
    <a href="/2017/02/02/2017-12-22-yolo教程/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">下一篇</strong>
      <div class="article-nav-title">Yolo darknet训练自己的数据集教程(Newest 2016.12.23)</div>
    </a>
  
</nav>

  
</article>

<!-- Table of Contents -->

  <aside id="toc-sidebar">
    <div id="toc" class="toc-article">
    <strong class="toc-title">文章目录</strong>
    
        <ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#上云自动化脚本"><span class="nav-number">1.</span> <span class="nav-text">上云自动化脚本</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#防火墙设置"><span class="nav-number">2.</span> <span class="nav-text">防火墙设置</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#nginx配置"><span class="nav-number">3.</span> <span class="nav-text">nginx配置</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#UPDATE-上云的一些错误集锦"><span class="nav-number"></span> <span class="nav-text">[UPDATE] 上云的一些错误集锦</span></a>
    
    </div>
  </aside>
</section>
        
      </div>
      
      <footer id="footer">
  

  <div class="container">
      	<div class="row">
	      <p> Powered by <a href="http://www.luoli-luoli.com/" target="_blank">萝莉萝莉</a> and <a href="http://www.luoli-luoli.com/sia" target="_blank">Sia</a> </p>
	      <p id="copyRightEn">Copyright &copy; 2017 - 2018 Jin Tian All Rights Reserved.</p>
	      
	      
    		<p class="busuanzi_uv">
				访客数 : <span id="busuanzi_value_site_uv"></span> |  
				访问量 : <span id="busuanzi_value_site_pv"></span>
		    </p>
  		   
		</div>

		
  </div>
</footer>


<!-- min height -->

<script>
    var wrapdiv = document.getElementById("wrap");
    var contentdiv = document.getElementById("content");
    var allheader = document.getElementById("allheader");

    wrapdiv.style.minHeight = document.body.offsetHeight + "px";
    if (allheader != null) {
      contentdiv.style.minHeight = document.body.offsetHeight - allheader.offsetHeight - document.getElementById("footer").offsetHeight + "px";
    } else {
      contentdiv.style.minHeight = document.body.offsetHeight - document.getElementById("footer").offsetHeight + "px";
    }
</script>
    </div>
    <!-- <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
    <a href="/categories" class="mobile-nav-link">Categories</a>
  
    <a href="/tags" class="mobile-nav-link">Tags</a>
  
    <a href="/about" class="mobile-nav-link">About</a>
  
    <a href="http://luoli-luoli.com/chat" class="mobile-nav-link">Chat</a>
  
</nav> -->
    

<!-- mathjax config similar to math.stackexchange -->

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    tex2jax: {
      inlineMath: [ ['$','$'], ["\\(","\\)"] ],
      processEscapes: true
    }
  });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
      tex2jax: {
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
    });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for(i=0; i < all.length; i += 1) {
            all[i].SourceElement().parentNode.className += ' has-jax';
        }
    });
</script>

<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/scripts.js"></script>




  <script src="/js/dialog.js"></script>








	<div style="display: none;">
    <script src="https://s95.cnzz.com/z_stat.php?id=1260716016&web_id=1260716016" language="JavaScript"></script>
  </div>



	<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
	</script>






  </div>

  <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h2 class="modal-title" id="myModalLabel">设置</h2>
      </div>
      <hr style="margin-top:0px; margin-bottom:0px; width:80%; border-top: 3px solid #000;">
      <hr style="margin-top:2px; margin-bottom:0px; width:80%; border-top: 1px solid #000;">


      <div class="modal-body">
          <div style="margin:6px;">
            <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" onclick="javascript:setFontSize();" aria-expanded="true" aria-controls="collapseOne">
              正文字号大小
            </a>
          </div>
          <div id="collapseOne" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingOne">
          <div class="panel-body">
            您已调整页面字体大小
          </div>
        </div>
      


          <div style="margin:6px;">
            <a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" onclick="javascript:setBackground();" aria-expanded="true" aria-controls="collapseTwo">
              夜间护眼模式
            </a>
        </div>
          <div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
          <div class="panel-body">
            夜间模式已经开启，再次单击按钮即可关闭 
          </div>
        </div>

        <div>
            <a data-toggle="collapse" data-parent="#accordion" href="#collapseThree" aria-expanded="true" aria-controls="collapseThree">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;关 于&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a>
        </div>
         <div id="collapseThree" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingThree">
          <div class="panel-body">
            Jin Tian
          </div>
          <div class="panel-body">
            Copyright © 2018 Jintian All Rights Reserved.
          </div>
        </div>
      </div>


      <hr style="margin-top:0px; margin-bottom:0px; width:80%; border-top: 1px solid #000;">
      <hr style="margin-top:2px; margin-bottom:0px; width:80%; border-top: 3px solid #000;">
      <div class="modal-footer">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
      </div>
    </div>
  </div>
</div>
  
  <a id="rocket" href="#top" class=""></a>
  <script type="text/javascript" src="/js/totop.js?v=1.0.0" async=""></script>
  
    <a id="menu-switch"><i class="fa fa-bars fa-lg"></i></a>
  
</body>
</html>